import { ADD_COUNTER, ADD_TO_CART } from './mutations-type';


export default{
    // context上下文 actions中必传参数
    addCart(context, payload) {

        // 1.查找之前数组中是否有该商品
        // item是将state.cartList中遍历的值
        let oldProduct = context.state.cartList.find(item => item.iid === payload.iid)

        if (oldProduct) {
            // oldProduct.count += 1
            // 调用mutations中的事件
            context.commit(ADD_COUNTER, oldProduct)
        } else {
            // oldProduct.count = 1
            // context.state.cartList.push(payload)
            payload.count = 1
            context.commit(ADD_TO_CART, payload)
        }
    },
}